//
//  Team9s_homeViewController.m
//  Team9Project
//
//  Created by dlios on 15/10/19.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9s_homeViewController.h"
#import "Team9SeeOneViewController.h"
#import "JQAFNetworkTool.h"
#import "Team9BaseLabel.h"
#import "Team9s_homeHeadModel.h"
#import "Team9s_homePicModel.h"
#import <Masonry.h>
#import "Team9s_homePicCell.h"
#import "Team9discussTableViewCell.h"
#import "Team9dairyDiscussCell.h"
#import "Team9diaryDiscussModel.h"
#import "Team9dairyDiscussCell.h"
@interface Team9s_homeViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *homeTablew;
@property (nonatomic,strong) Team9BaseView *headView;
@property (nonatomic,strong) UIImageView *coverImageView;
@property (nonatomic, strong) NSMutableArray *homePicArray;
@property (nonatomic, strong) NSMutableArray *homeDisArray;
@property (nonatomic, strong) Team9BaseLabel *titleLabel;
@property (nonatomic, strong) Team9BaseLabel *placeLabel;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) Team9BaseLabel *userNameLabel;
@property (nonatomic, strong) Team9BaseLabel *describleLabel;
@property (nonatomic,strong) Team9s_homeHeadModel *headModel;
@property (nonatomic, strong) NSMutableArray *picArray;



@end

@implementation Team9s_homeViewController

-(void)viewWillAppear:(BOOL)animated{
    if ([Team9EveningType shareEvening].eveningFailling) {
         self.describleLabel.backgroundColor = [UIColor blackColor];
        self.homeTablew.backgroundColor = [UIColor blackColor];
    }else{
      self.describleLabel.backgroundColor = [UIColor whiteColor];
      self.homeTablew.backgroundColor = [UIColor whiteColor];
    }

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title= @"晒家";
    
    
    
    [self creatTableView];
    self.empty = [Team9EmptyTool toolWithScrollView:self.homeTablew];
    [self createData];
   
    
    
    

}




#pragma mark 数据解析
- (void)createData{
    NSString *url = [NSString stringWithFormat:@"http://api.meilijia.com/app/collection_page?col_id=%@&tab=photo&page=1", self.modelId];
    
//    [JQAFNetworkTool getUrl:url body:nil result:JQJSON headerFile:nil success:^(id result) {
//        self.headModel = [[Team9s_homeHeadModel alloc] init];
//        [self.headModel setValuesForKeysWithDictionary:result[@"data"][@"collection_info"][@"info"]];
//        [self giveHeadValue];
//        
//        // 图片
//        self.homePicArray = [NSMutableArray array];
//        self.homePicArray = [Team9s_homePicModel baseModelByArr:result[@"data"][@"collection_photos"][@"photos"]].mutableCopy;
//
//        // 讨论
//        self.homeDisArray = [NSMutableArray array];
//        self.homeDisArray = [Team9diaryDiscussModel baseModelByArr:result[@"data"][@"collection_comments"][@"comments"]].mutableCopy;
//        
//        [self.homeTablew reloadData];
//        
//    } failure:^(NSError *error) {
//        
//        
//    }];
    
    
    [Team9RefreshTool getUrl:url success:^(id result) {
        self.headModel = [[Team9s_homeHeadModel alloc] init];
        [self.headModel setValuesForKeysWithDictionary:result[@"data"][@"collection_info"][@"info"]];
        [self giveHeadValue];
        
        // 图片
        self.homePicArray = [NSMutableArray array];
        self.homePicArray = [Team9s_homePicModel baseModelByArr:result[@"data"][@"collection_photos"][@"photos"]].mutableCopy;
        
        // 讨论
        self.homeDisArray = [NSMutableArray array];
        self.homeDisArray = [Team9diaryDiscussModel baseModelByArr:result[@"data"][@"collection_comments"][@"comments"]].mutableCopy;
        
        [self.homeTablew reloadData];
    } scrollView:self.homeTablew empty:self.empty];
    
}


- (void)giveHeadValue{
    
    self.titleLabel.text = self.headModel.col_name;
    self.placeLabel.text = self.headModel.title_sub_3;
    [Team9Tool sd_imageView:self.headImageView image:self.headModel.author_face placeholder:[UIImage imageNamed:@"111"]];
    [Team9Tool imageView:self.headImageView type:self.headModel.author_type Id:self.headModel.author_id viewController:self];
    
    self.userNameLabel.text = self.headModel.author_nick;
    [Team9Tool sd_imageView:self.coverImageView image:self.headModel.cover placeholder:[UIImage imageNamed:@"111"]];
    self.describleLabel.text = self.headModel.col_summary;
    CGFloat desHeight = [Team9Tool heightByFloatSize:13 / 375.0 * WIDTH width:WIDTH - 40 / 375.0 * WIDTH With:self.headModel.col_summary];
    
    
    self.headView.frame = CGRectMake(0, 0, WIDTH, desHeight + 190 / 375.0 * WIDTH);
    self.homeTablew.tableHeaderView = self.headView;
    
}
#pragma mark 创建tableView 和头视图

- (void)creatTableView{
    
    
    
    
    self.homeTablew = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEGHT - 64) style:UITableViewStylePlain];

    [self.view addSubview:self.homeTablew];
    self.homeTablew.delegate = self;
    self.homeTablew.dataSource = self;
    
    UIView * view = [[UIView alloc] init];
    self.homeTablew.tableFooterView = view;
    

    
    self.headView = [[Team9BaseView alloc] initWithType:ViewTYpeLight];

    self.headView.userInteractionEnabled = YES;
   
  
    
    self.coverImageView = [[UIImageView alloc] init];
    [self.headView addSubview:self.coverImageView];
    self.coverImageView.frame = CGRectMake(0, 0, WIDTH, 180 / 375.0 * WIDTH);

    
    self.titleLabel = [[Team9BaseLabel alloc] init];
    self.titleLabel.font = [UIFont systemFontOfSize:18 / 375.0 * WIDTH];
    [self.coverImageView addSubview:self.titleLabel];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    self.placeLabel = [[Team9BaseLabel alloc] init];
    self.placeLabel.font =[ UIFont systemFontOfSize:15 / 375.0 * WIDTH];
    [self.coverImageView addSubview:self.placeLabel];
    self.placeLabel.textColor = [UIColor whiteColor];
    self.placeLabel.textAlignment = NSTextAlignmentCenter;
    
    self.headImageView = [[UIImageView alloc] init];
    [self.coverImageView addSubview:self.headImageView];
    self.headImageView.layer.cornerRadius = 25 / 375.0 * WIDTH;
    self.headImageView.layer.masksToBounds = YES;
    self.coverImageView.userInteractionEnabled = YES;
   
    
    self.userNameLabel = [[Team9BaseLabel alloc] init];
    [self.coverImageView addSubview:self.userNameLabel];
    self.userNameLabel.font = [UIFont systemFontOfSize:13 / 375.0 * WIDTH];
    self.userNameLabel.textAlignment = NSTextAlignmentCenter;
    self.userNameLabel.textColor = [UIColor whiteColor];
    
    self.describleLabel = [[Team9BaseLabel alloc] init];
    [self.headView addSubview:self.describleLabel];

   
    self.describleLabel.font = [UIFont systemFontOfSize:13 / 375.0 * WIDTH];
    self.describleLabel.numberOfLines = 0;
    [self.describleLabel sizeToFit];
    
    
    

    
    
    [self make_Constraints];
}


- (void)make_Constraints{
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.coverImageView.mas_centerX);
        make.centerY.equalTo(self.coverImageView.mas_centerY).offset(-40 / 375.0 * WIDTH);
        make.width.equalTo(self.coverImageView.mas_width).offset(-40 / 375.0 * WIDTH);
        make.height.mas_equalTo(30 / 375.0 * WIDTH);    }];
    


    [self.placeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.coverImageView.mas_centerX);
        make.centerY.equalTo(self.coverImageView.mas_centerY).offset(-10 / 375.0 * WIDTH);
        make.width.equalTo(self.coverImageView.mas_width).offset(-40 / 375.0 * WIDTH);
        make.height.mas_equalTo(25 / 375.0 * WIDTH);
        
    }];

    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.coverImageView.mas_centerX);
        make.centerY.equalTo(self.coverImageView.mas_centerY).offset(30 / 375.0 * WIDTH);
        make.width.mas_equalTo(50 / 375.0 * WIDTH);
        make.height.equalTo(self.headImageView.mas_width);
        
    }];
    
    [self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.coverImageView.mas_centerX);
        make.top.mas_equalTo(self.headImageView.mas_bottom).offset(5 / 375.0 * WIDTH);
        make.width.equalTo(self.coverImageView.mas_width).offset(-40 / 375.0 * WIDTH);
        make.height.mas_equalTo(18 / 375.0 * WIDTH);
        
    }];
    
    
    [self.describleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headView.mas_left).offset(10 / 375.0 * WIDTH);
        make.top.mas_equalTo(self.coverImageView.mas_bottom).offset(10 / 375.0 * WIDTH);
        make.right.equalTo(self.headView.mas_right).offset(-10 / 375.0 * WIDTH);
        make.bottom.equalTo(self.headView.mas_bottom);
        
    }];
    
    
}



#pragma mark tableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if (section == 0) {
        return self.homePicArray.count;
    }else{
        return self.homeDisArray.count;
    }
    
   
    
}

// 分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}



- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"毕业照";
        
    }else{
        return @"评论";
        
    }
    
    
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        if (self.homePicArray.count == 0) {
            return 0;
        }
    }else{
        if (self.homeDisArray.count == 0) {
            return 0;
        }
    }

    return 30 / 375.0 * WIDTH;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        Team9s_homePicModel *model = self.homePicArray[indexPath.row];
        CGFloat contentDesHeight = [Team9Tool heightByFloatSize:13 / 375.0 * WIDTH width:WIDTH - 20 / 375.0 * WIDTH With:model.col_remark];

        
        
        if (model.pic != nil) {
            
            CGFloat a = [model.pic_height floatValue];
            CGFloat b = [model.pic_width floatValue];
            CGFloat proportion = a / b;
            CGFloat contentPicHeight = proportion * (WIDTH - 20 / 375.0 * WIDTH);
            
            return contentDesHeight + contentPicHeight + 30 / 375.0 * WIDTH;
          
            
        }else{
            
            return contentDesHeight + 30 / 375.0 * WIDTH;
        
        }
            
            
    }else{
        
        
        if (self.homeDisArray.count != 0) {
            Team9diaryDiscussModel *disModel = self.homeDisArray[indexPath.row];
            CGFloat contentHeight = [Team9Tool heightByFloatSize:13 / 375.0 * WIDTH width:WIDTH - 40 / 375.0 * WIDTH With:disModel.content];
            
            return contentHeight + 80 / 375.0 * WIDTH;
            
        }else{
        
            return 20 / 375.0 * WIDTH;
        }

    
    }

}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        static NSString *reuse = @"reuse";
        Team9s_homePicCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell= [[Team9s_homePicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        
        if (self.homePicArray != nil) {
            Team9s_homePicModel *picModel = self.homePicArray[indexPath.row];
            cell.picModel = picModel;
//            cell.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
            
        }

      
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        
      
        void (^Block)( ) = ^(){
            
            self.picArray = @[].mutableCopy;
            
            Team9SeeOneViewController *seeVC = [[Team9SeeOneViewController alloc] init];
            
            

            if (self.homePicArray != nil) {
                for (NSInteger con = 0; con <self.homePicArray.count; con ++) {
                    
                    if ([self.homePicArray[con] pic]!= nil) {
                        [self.picArray addObject:[self.homePicArray[con] pic]];
                    }
                    
                }

            }
            
            seeVC.picArray = [NSArray arrayWithArray:self.picArray];
            seeVC.number = indexPath.row;

            [self.navigationController presentViewController:seeVC animated:YES completion:^{
                
                
            }];
            
            
        };
        cell.block = Block;

        
        
        return  cell;
    }else{
        
        static NSString *reuse = @"Reuse";
        Team9dairyDiscussCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell= [[Team9dairyDiscussCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        
        if (self.homeDisArray != nil) {
            Team9diaryDiscussModel *disModel = self.homeDisArray[indexPath.row];
            cell.disModel = disModel;
        }
        
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return  cell;
        
    }
    
    
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
